package com.ynhenc.comm;

import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Image;
import java.awt.image.*;
import java.net.*;
import javax.imageio.*; 

import com.ynhenc.comm.util.*;

public class GisComLib implements KernelInterface {
	
	public static String getMyDocuments() {
		return System.getProperty("user.home") + "\\My Documents\\";
	}

	public final void debug(Exception e) {
		debug.println(this, e);
	}

	public final void debug(String msg) {
		debug.println(this, msg);
	}

	public final void debug(StringBuffer msg) {
		debug.println(this, msg.toString());
	}

	public final void message(String message) {
		debug.println(this, message);
	}

	public Color getColor(String color) {
		return HtmlUtil.fromHtmlColor(color);
	}

	public Color getColor(String name, Color def) {
		Color color = this.getColor(name);
		return (color != null) ? color : def;
	}

	public String toColorText(Color color) {
		return HtmlUtil.toHtmlColor(color);
	}

	public final boolean isGood(Object obj) {
		return obj != null && obj.toString().length() > 0;
	}

	public final boolean isGood(String text) {
		return text != null && text.length() > 0;
	}

	public boolean isGood(Integer i) {
		return (i != null);
	}

	public boolean equalsStringTrimIgnoreCase(String a, String b) {
		if (a == b) {
			return true;
		} else if (a != null && b != null) {
			return a.trim().equalsIgnoreCase(b.trim());
		}
		return false;
	}

	public void messageDialog(Component comp, String message) {
		this.messageDialog(comp, message, message);
	}

	public void messageDialog(Component comp, String message, String title) {
		this.showOptionPaneDialog(comp, message, title, JOptionPane.INFORMATION_MESSAGE);
	}

	public void warnDialog(Component comp, String message) {
		this.warnDialog(comp, message, message);
	}

	public void warnDialog(Component comp, String message, String title) {
		this.showOptionPaneDialog(comp, message, title, JOptionPane.WARNING_MESSAGE);
	}

	public void showOptionPaneDialog(Component comp, String message, String title, int type) {
		JOptionPane.showMessageDialog(comp, message, title, type);
	}

	public void invokeLater(Runnable runnable) {
		javax.swing.SwingUtilities.invokeLater(runnable);
	}  

	// static methods

	public static BufferedImage convertToBufferedImage(Image image) {

		if (image == null) {
			return null;
		} else {

			if (image == null) {
				return null;
			}

			Frame f = GisComLib.getFrame();

			int w = image.getWidth(f), h = image.getHeight(f);
			BufferedImage bImage;

			bImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
			bImage.getGraphics().drawImage(image, 0, 0, f);

			return bImage;
		}
	}

	private static Frame getFrame() {
		if( frame == null ) {
			frame = new Frame();
			frame.addNotify();
		}
		return frame;
	}

	private static Frame frame ;  
	
	public static final String NL = System.getProperty("line.separator");

	public static final String newLine = System.getProperty("line.separator");

}
